package com.team2.outpatient_sys.web.login;

import com.team2.outpatient_sys.pojo.Administrator;
import com.team2.outpatient_sys.pojo.Doctor;
import com.team2.outpatient_sys.pojo.Result;
import com.team2.outpatient_sys.pojo.User;
import com.team2.outpatient_sys.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 管理员登录请求的处理方法
     * @param administrator 登录请求中管理员Json数据的封装
     * @param response 登录请求的HttpServletResponse响应对象
     * @return 响应内容，响应的登录结果的封装对象Result
     */
    @PostMapping("/a/login")
    public Result login(@RequestBody Administrator administrator, HttpServletResponse response){
        String token = loginService.login(administrator);
        response.setHeader("token", token);
        response.setHeader("Access-Control-Expose-Headers", "token");

        return new Result(200, "登录成功");
    }

    /**
     * 医护人员登录请求的处理方法
     * @param doctor 登录请求中医护人员Json数据的封装
     * @param response 登录请求的HttpServletResponse响应对象
     * @return 响应内容，响应的登录结果的封装对象Result
     */
    @PostMapping("/d/login")
    public Result login(@RequestBody Doctor doctor, HttpServletResponse response){
        String token = loginService.login(doctor);
        response.setHeader("token", token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return new Result(200, "登录成功");
    }

    /**
     * 病人用户登录请求的处理方法
     * @param user 登录请求中病人用户Json数据的封装
     * @param response 登录请求的HttpServletResponse响应对象
     * @return 响应内容，响应的登录结果的封装对象Result
     */
    @PostMapping("/u/login")
    public Result login(@RequestBody User user, HttpServletResponse response){
        String token = loginService.login(user);
        response.setHeader("token", token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return new Result(200, "登录成功");
    }
}
